﻿using GWSoft.Framework.Core.Extensions;
using GWSoft.Framework.Core.Helpers;
using GWSoft.Framework.Core.Models;
using GWSoft.Framework.WebSite.IServices;
using GWSoft.Framework.WebSite.Models;
using GWSoft.Framework.WebSite.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace GWSoft.Framework.WebSite.Controllers
{
    /// <summary>
    /// 组织机构控制类
    /// </summary>
    [Route("sys/orgs")]
    public class OrganizeController : Controller
    {
        private IOrganizeService iOrganizeService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="organizeService"></param>
        public OrganizeController(IOrganizeService organizeService)
        {
            if (organizeService == null)
                throw new ArgumentNullException(nameof(organizeService));

            iOrganizeService = organizeService;
        }

        /// <summary>
        /// 组织机构查询
        /// </summary>
        /// <param name="pagingParam">查询对象</param>
        /// <remarks>分页查询组织机构列表</remarks>
        /// <response code="200">组织机构查询成功</response>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Get(PagingParam pagingParam)
        {
            if(pagingParam == null)
            {
                return Ok(iOrganizeService.Get());
            }
            Expression<Func<Organize, bool>> exp = w => 1 == 1;
            if (!string.IsNullOrEmpty(pagingParam.queryParam))
            {
                Dictionary<string, string> keyValuePairs = pagingParam.queryParam.Split(new string[] { "&" },
                StringSplitOptions.RemoveEmptyEntries)
                .ToDictionary(m => m.Split(new string[] { "=" }, StringSplitOptions.None)[0],
                m => m.Split(new string[] { "=" }, StringSplitOptions.None)[1]);
                foreach (KeyValuePair<string, string> keyValuePair in keyValuePairs)
                {
                    if(keyValuePair.Key.Equals(nameof(Organize.Id), StringComparison.OrdinalIgnoreCase))
                    {
                        exp = exp.And(w => w.Id == keyValuePair.Value);
                        continue;
                    }
                    if(keyValuePair.Key.Equals(nameof(Organize.OrgCode), StringComparison.OrdinalIgnoreCase))
                    {
                        exp = exp.And(w => w.OrgCode == keyValuePair.Value);
                        continue;
                    }
                }
            }
            IList<Organize> orgList = ((OrganizeService)iOrganizeService).GetByPagination(exp, pagingParam.pageSize, (pagingParam.page - 1) * pagingParam.pageSize, true, od => od.OrderNo);
            return Ok(orgList);
        }

        /// <summary>
        /// 获取一个组织机构
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        [HttpGet("{orgid}")]
        public IActionResult Get(string orgId)
        {
            var organize = iOrganizeService.GetSingle(orgId);
            if(organize == null)
            {
                return NotFound();
            }

            return Ok(organize);
        }

        /// <summary>
        /// 新增组织机构
        /// </summary>
        /// <param name="organize"></param>
        /// <returns></returns>
        [Authorize]
        [HttpPost]
        public IActionResult Post([FromBody]Organize organize)
        {
            iOrganizeService.Add(organize);
            return Created($"sys/orgs/{organize.Id}", organize);
        }

        /// <summary>
        /// 更新组织机构
        /// </summary>
        /// <param name="orgId"></param>
        /// <param name="organize"></param>
        /// <returns></returns>
        [HttpPut("{orgid}")]
        public IActionResult Put(string orgId, [FromBody]Organize organize)
        {
            var _organize = iOrganizeService.GetSingle(orgId);
            if (_organize == null)
                return NotFound();
            _organize.OrgCode = organize.OrgCode;
            _organize.OrgName = organize.OrgName;
            _organize.Comments = organize.Comments;
            _organize.AreaCode = organize.AreaCode;
            iOrganizeService.Update(_organize);
            return Created($"sys/orgs/{_organize.Id}", _organize);
        }

        /// <summary>
        /// 删除组织机构
        /// </summary>
        /// <param name="orgId"></param>
        /// <returns></returns>
        [HttpDelete("{orgid}")]
        public IActionResult Delete(string orgId)
        {
            var _organize = iOrganizeService.GetSingle(orgId);
            if (_organize == null)
                return NotFound();

            iOrganizeService.Delete(orgId);

            return NoContent();
        }
    }
}
